package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreFileLogic;
import cn.topevery.garden.dal.logic.MaintenanceFileLogic;
import cn.topevery.garden.dal.logic.MaintenanceRecordLogic;
import cn.topevery.garden.entity.dto.MaintenanceRecordDTO;
import cn.topevery.garden.entity.po.MaintenanceRecord;
import cn.topevery.garden.entity.query.MaintenanceRecordQuery;
import cn.topevery.garden.entity.vo.MaintenanceRecordVO;
import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingException;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 保养记录
 *
 * @author code-generate
 * @date 2022-02-17
 */
@Api(tags = "保养记录")
@RestController
@RequestMapping("/maintenanceRecord")
public class MaintenanceRecordController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    MaintenanceRecordLogic maintenanceRecordLogic;

    @Autowired
    MaintenanceFileLogic maintenanceFileLogic;

    @Autowired
    GreFileLogic greFileLogic;

    /**
     * 获取保养记录分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取保养记录页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<MaintenanceRecordVO>> listPage(@RequestBody MaintenanceRecordQuery query) {
        DataPager<MaintenanceRecordVO> page = maintenanceRecordLogic.listPage(query);
        List<MaintenanceRecordVO> records = page.records;
        if (records != null && !records.isEmpty()) {
            records.stream().peek(temp -> {
                temp.setFileList(greFileLogic.getListByMainId(temp.getTreeId(), 3));
            }).collect(Collectors.toList());
        }
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 保养记录信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody MaintenanceRecordDTO dto) {
        MaintenanceRecord maintenanceRecord = mapper.map(dto, MaintenanceRecord.class);
        String userId = getUser().id.toString();
        try {
            maintenanceRecord.setDate(LocalDateTime.now());
            maintenanceRecord.setRecordCode(System.currentTimeMillis()+"");
            maintenanceRecordLogic.saveOrUpdate(userId, maintenanceRecord);
            return JsonResult.ok(true);
        } catch (MappingException e) {
            e.printStackTrace();
            return JsonResult.ok(false);
        }
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<MaintenanceRecordVO> getInfoById(@RequestParam Integer id) {
        MaintenanceRecord entity = maintenanceRecordLogic.getEntity(id);
        MaintenanceRecordVO vo = mapper.map(entity, MaintenanceRecordVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        maintenanceRecordLogic.deleteBatchById(getUser().id.toString(), ids);
        maintenanceFileLogic.deleteByMainIds(ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出保养记录信息")
    @GetMapping("/export")
    public void export(MaintenanceRecordQuery query, HttpServletResponse response) {
        List<MaintenanceRecordVO> list = maintenanceRecordLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "保养记录", "sheet1", MaintenanceRecordVO.class, "保养记录", response);
    }

}
